﻿#include "TurtleKing.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;
using namespace Server::MirEnvir;

namespace Server::MirObjects::Monsters
{

    TurtleKing::TurtleKing(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool TurtleKing::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void TurtleKing::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        if (Stats[Stat::HP] >= 5)
        {
            unsigned char stage = static_cast<unsigned char>(HP / (Stats[Stat::HP] / 5));

            if (stage < _stage)
            {
                SpawnSlaves();
                _stage = stage;
            }
        }

        MonsterObject::ProcessAI();
    }

    void TurtleKing::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), CloseRange);

        if (!ranged)
        {
            switch (getEnvir()->Random->Next(5))
            {
                case 0:
                case 1:
                    S::ObjectAttack *tempVar = new S::ObjectAttack();
                    tempVar->ObjectID = ObjectID;
                    tempVar->Direction = getDirection();
                    tempVar->Location = getCurrentLocation();
                    tempVar->Type = 0;
                    Broadcast(tempVar);
                    LineAttack(2, 300);
                    break;
                case 2:
                case 3:
                    S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                    tempVar2->ObjectID = ObjectID;
                    tempVar2->Direction = getDirection();
                    tempVar2->Location = getCurrentLocation();
                    tempVar2->Type = 1;
                    Broadcast(tempVar2);
                    LineAttack(3, 300);
                    break;
                case 4:
                {
                    S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
                    tempVar3->ObjectID = ObjectID;
                    tempVar3->Direction = getDirection();
                    tempVar3->Location = getCurrentLocation();
                    tempVar3->Type = 1;
                    Broadcast(tempVar3);

                    int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    if (damage == 0)
                    {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                        return;
                    }

                    DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MAC});
                    ActionList.push_back(action);
                    break;

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            if (!Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), CloseRange) && getEnvir()->Random->Next(4) == 0)
            {
                S::ObjectRangeAttack *tempVar4 = new S::ObjectRangeAttack();
                tempVar4->ObjectID = ObjectID;
                tempVar4->Direction = getDirection();
                tempVar4->Location = getCurrentLocation();
                tempVar4->Type = 0;
                Broadcast(tempVar4);

                Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), 1);
                getTarget()->Teleport(getCurrentMap(), target, true, 6);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
            }
            else if (!Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), CloseRange) && getEnvir()->Random->Next(4) == 0)
            {
                S::ObjectRangeAttack *tempVar5 = new S::ObjectRangeAttack();
                tempVar5->ObjectID = ObjectID;
                tempVar5->Direction = getDirection();
                tempVar5->Location = getCurrentLocation();
                tempVar5->Type = 2;
                Broadcast(tempVar5);

                Point *target = Functions::PointMove(getTarget()->getCurrentLocation(), getTarget()->getDirection(), 1);
                Teleport(getCurrentMap(), target, true, 6);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar5' statement was not added since tempVar5 was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectRangeAttack *tempVar6 = new S::ObjectRangeAttack();
                tempVar6->ObjectID = ObjectID;
                tempVar6->Direction = getDirection();
                tempVar6->Location = getCurrentLocation();
                tempVar6->Type = 1;
                Broadcast(tempVar6);

                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar6' statement was not added since tempVar6 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MAC});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar6' statement was not added since tempVar6 was passed to a method or constructor. Handle memory management manually.
            }
        }
        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
    }

    void TurtleKing::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (target->Attacked(this, damage, defence) <= 0)
        {
            return;
        }

        PoisonTarget(target, 8, 3, PoisonType::Dazed, 1000);
    }

    void TurtleKing::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(AttackRange, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            S::ObjectEffect *tempVar = new S::ObjectEffect();
            tempVar->ObjectID = getTarget()->ObjectID;
            tempVar->Effect = SpellEffect::TurtleKing;
            Broadcast(tempVar);
            if (targets[i]->Attacked(this, damage, DefenceType::MAC) <= 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            PoisonTarget(targets[i], 5, 15, PoisonType::Slow, 1000);
            PoisonTarget(targets[i], 5, 5, PoisonType::Paralysis, 1000);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void TurtleKing::SpawnSlaves()
    {
        int count = std::min(8, 30 - SlaveList.size());

        for (int i = 0; i < count; i++)
        {
            MonsterObject *mob = nullptr;
            switch (getEnvir()->Random->Next(7))
            {
                case 0:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::Turtle1));
                    break;
                case 1:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::Turtle2));
                    break;
                case 2:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::Turtle3));
                    break;
                case 3:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::Turtle4));
                    break;
                case 4:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::Turtle5));
                    break;
            }

            if (mob == nullptr)
            {
                continue;
            }

            if (!mob->Spawn(getCurrentMap(), getFront()))
            {
                mob->Spawn(getCurrentMap(), getCurrentLocation());
            }

            //mob.Master = this;
            mob->setTarget(getTarget());
            mob->ActionTime = getEnvir()->getTime() + 2000;
            SlaveList.push_back(mob);
        }
    }
}
